package com.sfh.dog.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.entity.GiftEntity;
import com.sfh.dog.http.APIConfig;

/**
 * 礼物
 * 
 * @author SunFeihu 孙飞虎
 * @version [版本号2014-3-28]
 */
public class GiftAdapter extends BaseAdapter
{
	private List<GiftEntity> mEntities;

	public void setEntities(List<GiftEntity> data)
	{
		this.mEntities.clear();
		if (null != data && data.size() > 0)
		{
			this.mEntities.addAll(data);
			notifyDataSetChanged();
		}

	}

	private LayoutInflater mInflater;

	public GiftAdapter(Context context)
	{
		super();
		this.mInflater = LayoutInflater.from(context);
		this.mEntities = new ArrayList<GiftEntity>();

	}

	@Override
	public int getCount()
	{
		return mEntities.size();
	}

	@Override
	public Object getItem(int arg0)
	{
		return mEntities.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2)
	{
		HondlerView mHondlerView = null;
		if (arg1 == null)
		{
			arg1 = mInflater.inflate(R.layout.gift_item, arg2, false);
			mHondlerView = new HondlerView();
			mHondlerView.ivPhoto = (ImageView) arg1.findViewById(R.id.ivPhoto);
			mHondlerView.tvName = (TextView) arg1.findViewById(R.id.tvName);
			mHondlerView.tvMoney = (TextView) arg1
					.findViewById(R.id.tvMoney);
			arg1.setTag(mHondlerView);
		}
		mHondlerView = (HondlerView)arg1.getTag();
		GiftEntity entity = mEntities.get(arg0);
		mHondlerView.tvName.setText(entity.name);
		setTrem(entity, mHondlerView.tvMoney);
		ImageLoader.getInstance().displayImage(
				APIConfig.getGiftURL(entity.pathID),
				mHondlerView.ivPhoto, DogApplication.options);
		return arg1;
	}

	void setTrem(GiftEntity entity, TextView tv)
	{
		switch (entity.term)
		{
			case 0:
				tv.setText("无限制");
				break;
			case 1:
				tv.setText(String.format("等级限制:%s", entity.grade));
				break;

			case 2:
				tv.setText(String.format("积分限制:%s", entity.grade));
				break;
			case 3:
				tv.setText(String.format("Vip限制:%s", entity.grade));
				break;
			default:

				break;
		}
	}
	static class HondlerView
	{
		public ImageView ivPhoto;
		public TextView tvName;
		public TextView tvMoney;

	}
}
